package com.base.cn.platform.os.controller.classes.topic.like;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.classes.topic.ClassesTopic;
import com.base.cn.platform.os.entity.classes.topic.like.ClassesTopicLike;
import com.base.cn.platform.os.service.classes.member.ClassesMemberBiz;
import com.base.cn.platform.os.service.classes.topic.ClassesTopicBiz;
import com.base.cn.platform.os.service.classes.topic.like.ClassesTopicLikeBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 班级话题点赞
 *
 * @author SK
 * @since 2018-05-30
 */
@RestController
@RequestMapping("/course")
public class ClassesTopicLikeController extends BaseController {

    @Autowired
    private ClassesTopicBiz classesTopicBiz;
    @Autowired
    private ClassesTopicLikeBiz classesTopicLikeBiz;
    @Autowired
    private ClassesMemberBiz classesMemberBiz;

    /**
     * 查询班级话题点赞列表
     *
     * @param like    话题点赞查询条件
     * @param count   查询数量
     * @param setUser 设置用户
     * @return 班级话题点赞列表
     */
    @RequestMapping("/findClassesTopicLikeList")
    public List<ClassesTopicLike> findClassesTopicLikeList(@RequestBody ClassesTopicLike like,
                                                           @RequestParam(value = "count", required = false) Integer count,
                                                           @RequestParam("setUser") boolean setUser) {

        return classesTopicLikeBiz.findClassesTopicLikeList(like, count, setUser);
    }

    /**
     * 保存班级话题点赞
     *
     * @param topicId 话题id
     * @param userId  用户id
     * @return 保存结果
     */
    @RequestMapping("/saveClassesTopicLike")
    public Map<String, Object> saveClassesTopicLike(@RequestParam("topicId") BigDecimal topicId,
                                                    @RequestParam("userId") BigDecimal userId) {
        Map<String, Object> check = this.checkParams(topicId, userId);
        if (check != null) {
            return check;
        }
        ClassesTopicLike like = new ClassesTopicLike();
        like.setTopicId(topicId);
        like.setUserId(userId);
        return classesTopicLikeBiz.saveClassesTopicLike(like);
    }

    /**
     * 验证用户是点赞过话题
     * @param userId 用户ID
     * @param topicId 话题 ID
     * @return false未点赞，true已点赞
     */
    @RequestMapping("/checkUserLikeTopic")
    public boolean checkUserLikeTopic(@RequestParam("userId") BigDecimal userId,
                                      @RequestParam("topicId") BigDecimal topicId){
        // 是否点过赞
        ClassesTopicLike classesTopicLike = new ClassesTopicLike();
        classesTopicLike.setTopicId(topicId);
        classesTopicLike.setUserId(userId);
        List<ClassesTopicLike> one = classesTopicLikeBiz.findClassesTopicLikeList(classesTopicLike, 1, false);
        return ObjectUtils.isNotEmpty(one);
    }

    private Map<String, Object> checkParams(BigDecimal topicId, BigDecimal userId) {
        if (!(DataUtil.idIsNotNull(topicId) && DataUtil.idIsNotNull(userId))) {
            return ResultUtil.ERROR("参数错误");
        }
        ClassesTopic topic = classesTopicBiz.findClassesTopicById(topicId,false,true,false,false);
        boolean isBanned = classesMemberBiz.isBanned(topic.getClassesId(),userId);
        if(!isBanned){
            return ResultUtil.ERROR("您已被班级禁止点赞");
        }
        return null;
    }
}
